package com.icss.train_sys_back.controller.admin.train_manage;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.icss.train_sys_back.entity.ResponseResult;
import com.icss.train_sys_back.entity.ResultEnum;
import com.icss.train_sys_back.entity.SeatEntity;
import com.icss.train_sys_back.entity.train_manage.TrainCarriageEntity;
import com.icss.train_sys_back.entity.train_manage.TrainEntity;
import com.icss.train_sys_back.mapper.train_manage.TrainCarriageManageMapper;
import com.icss.train_sys_back.mapper.train_manage.TrainMapper;
import com.icss.train_sys_back.service.train_manage.TrainCarriageManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.SecureRandom;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Author 小泽
 * @createTime 2025/9/25 16:20
 */
@RestController
@RequestMapping("/admin/train_carriage_manage")
@Slf4j
public class TrainCarriageManageController {

    @Autowired
    private TrainCarriageManageService trainCarriageManageService;


    @Autowired
    private TrainMapper trainMapper;
    @Autowired
    private TrainCarriageManageMapper trainCarriageManageMapper;

    //车厢列表+分页+条件查询（如果有）
    @GetMapping("/getList")
    public ResponseResult findListConditions(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String trainNumber,
            @RequestParam(required = false) String carriageNumber,
            @RequestParam(required = false) Integer carriageType) {

        log.info("车厢列表+分页+条件查询" +
                        " page = {},pageSize = {},trainNumber = {},carriageNumber = {},carriageType = {},",
                page, pageSize, trainNumber, carriageNumber, carriageType);
        PageHelper.startPage(page, pageSize);
        List<TrainCarriageEntity> trainCarriageEntityList = trainCarriageManageService.findListConditions(trainNumber, carriageNumber, carriageType);
        log.info("查询结果 = {}", trainCarriageEntityList);
        PageInfo<TrainCarriageEntity> pageInfo = new PageInfo<>(trainCarriageEntityList);
        return ResponseResult.success(pageInfo);
    }

    //加载车辆的车辆号
    @PostMapping("/loadTrainList")
    public ResponseResult loadTrainList() {
        log.info("加载车辆号方法");
        List<TrainEntity> trainEntityList = trainCarriageManageService.findTrainList();
        return ResponseResult.success(trainEntityList);
    }

    //车厢添加
    @PostMapping("/add")
    public ResponseResult add(@RequestBody TrainCarriageEntity trainCarriage) {
        log.info("车厢添加方法,参数 = {}", trainCarriage);
        boolean res = trainCarriageManageService.add(trainCarriage);
        return res ? ResponseResult.success() : ResponseResult.fail(ResultEnum.FAIL);
    }

    //车厢更新
    @PostMapping("/update")
    public ResponseResult update(@RequestBody TrainCarriageEntity trainCarriage) {
        log.info("车厢更新 表单数据 = {}", trainCarriage);
        boolean res = trainCarriageManageService.update(trainCarriage);
        return res ? ResponseResult.success() : ResponseResult.fail(ResultEnum.FAIL);
    }

    //根据 id 删除车厢
    @DeleteMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable("id") Integer id) {
        log.info("根据 id 删除车厢  id = {}", id);
        boolean res = trainCarriageManageService.delete(id);
        return res ? ResponseResult.success() : ResponseResult.fail(ResultEnum.FAIL);
    }

    //获取所有车厢 类型
    @GetMapping("/getCarriageTypes")
    public ResponseResult getCarriageTypes() {
        log.info("获取所有车厢类型");
        List<TrainCarriageEntity> trainCarriageEntityList = trainCarriageManageService.getAllCarriageTypes();
        return ResponseResult.success(trainCarriageEntityList);
    }

    @GetMapping("/addSeatTest")
    public void addSeat() {
        List<SeatEntity> seatEntityList = new ArrayList<>();
        //获取所有车辆 -- 获得车辆号
        //车辆号查询所有车厢号
        List<TrainEntity> allTrains = trainMapper.findAllTrains();
        for (TrainEntity train : allTrains) {
            List<TrainCarriageEntity> allCarriages =
                    trainCarriageManageMapper.findListConditions(train.getTrainNumber(), null, null);
            for (TrainCarriageEntity carriage : allCarriages) {
                Integer seatCount = carriage.getSeatCount();
                for (int i = 0; i < seatCount; i++) {
                    Random random = new Random();
                    Integer type = random.nextInt(5) + 1;
                    SeatEntity seat = new SeatEntity(null, carriage.getCarriageNumber(), train.getTrainNumber(), generateRandomSeatNumber(), type, "F", 0, LocalDateTime.now());
                    seatEntityList.add(seat);
                }
            }
        }
        log.info("座位数{}", seatEntityList.size());
        log.info("{}", seatEntityList);

        trainCarriageManageService.addSeats(seatEntityList);
    }

    @GetMapping("/addTrainCarriage")
    public void addTrainCarriage() {
        List<TrainCarriageEntity> carriageEntityList = new ArrayList<>();
        List<TrainEntity> allTrains = trainMapper.findAllTrains();
        Random random = new Random();
        for (TrainEntity train : allTrains) {
            //每个火车十节车厢
            for (int i = 0; i < 10; i++) {
                TrainCarriageEntity trainCarriage = new TrainCarriageEntity(null, train.getTrainNumber(), random.nextInt(15) + 1, random.nextInt(5), random.nextInt(50) + 50, random.nextInt(20), LocalDateTime.now());
                carriageEntityList.add(trainCarriage);
            }
        }
        trainCarriageManageService.addCarriages(carriageEntityList);
    }

    /**
     * 生成随机车座号的方法
     * 格式：数字(1-18) + 字母(A-F)
     * 示例：1A, 18F, 3B, 12C
     *
     * @return 随机座位号字符串
     */
    public static String generateRandomSeatNumber() {
        // 使用安全的随机数生成器
        Random random = new SecureRandom();

        // 座位字母范围 A-F
        char[] seatLetters = {'A', 'B', 'C', 'D', 'E', 'F'};

        // 生成1-18的随机数字
        int number = random.nextInt(18) + 1;

        // 随机选择一个字母
        char letter = seatLetters[random.nextInt(seatLetters.length)];

        // 返回组合的座位号
        return number + String.valueOf(letter);
    }

}
